<style lang="less">
@import "../../../assets/style/xinzheng.less";
</style>

<template>
	<mxq-router-layout>
		<div>
			<Head slot="header" @headerClick="headerClick($event)">
				<div slot="headerTitle">
					<span>新增隐患</span>
				</div>
				<div slot="headerRight">
					<i class="iconfont icon-shouye"/>
					<!--<van-icon class="home" name="home"/>-->
				</div>
			</Head>
			<div class="indexBox">
				<adminHome :is="currentHome" @upLoad="upLoad"></adminHome>
			</div>
		</div>
	</mxq-router-layout>
</template>

<script>
import { fileData,callData,all } from '../../../axios/axios';
import Head from '@/components/head';
import shuzhangXinZheng from '../../components/yinhuan/shuzhang_yinhuanXinZheng.vue';
import jianzhuXinZheng from '../../components/yinhuan/jianzhu_yinhuanXinZheng.vue';
import jckyXinZheng from '../../components/yinhuan/jcky_yinhuanXinZheng.vue';
import yiwuXinZheng from '../../components/yinhuan/yiwu_yinhuanXinZheng.vue';
import shigongXinZheng from '../../components/yinhuan/shigong_yinhuanXinZheng.vue';
import xiaofangXinZheng from '../../components/yinhuan/xiaofang_yinhuanXinZheng.vue';
import fangxunXinZheng from '../../components/yinhuan/fangxun_yinhuanXinZheng.vue';
import otherXinZheng from '../../components/yinhuan/other_yinhuanXinZheng.vue';
import {
	Icon,
	Toast 
} from 'vant';
export default{
	components:{
		Head,
		shuzhangXinZheng,
		jianzhuXinZheng,
		jckyXinZheng,
		yiwuXinZheng,
		shigongXinZheng,
		xiaofangXinZheng,
		fangxunXinZheng,
		otherXinZheng,
		[Icon.name]: Icon,
		[Toast.name]: Toast
	},
	data(){
		return {
			screenWidth: document.documentElement.clientWidth,
			screenHeight: document.documentElement.clientHeight,
			currentHome: null
		}
	},
	methods: {
		headerClick(e){
			let query = {}
			this.$router.push({
				name:'index',
				query: query
			})
		},
		homeShow(){
			var yinhuanIndex = this.$route.query.yhId;
			//这里根据传进来的参数不一样，从而判断显示哪个页面
        	if(yinhuanIndex == '503988636121874432'){	//树障
        		this.currentHome = shuzhangXinZheng;
        	}else if(yinhuanIndex == '503989129904701440'){	//违章建筑物
        		this.currentHome = jianzhuXinZheng;
        	}else if(yinhuanIndex == '503989282900328448'){	//交叉跨越隐患
        		this.currentHome = jckyXinZheng;
        	}else if(yinhuanIndex == '503989531907768320'){	// 异物隐患
        		this.currentHome = yiwuXinZheng;
        	}else if(yinhuanIndex == '503989671221575680'){	//施工外破
        		this.currentHome = shigongXinZheng;
        	}else if(yinhuanIndex == '503989986297692160'){	//消防隐患
        		this.currentHome = xiaofangXinZheng;
        	}else if(yinhuanIndex == '503990076341010432'){	//	防汛隐患
        		this.currentHome = fangxunXinZheng
        	}else {	//其他隐患503990222193737728
        		this.currentHome = otherXinZheng
        	}
		},
		upLoad(data, files){
			const _this = this
			data['yhLb'] =  this.$route.query.yhId 
			data['lineId'] =  this.$route.query.lineId
			data['towerId'] =  this.$route.query.towerId
			data['defectStatus'] =  0//0新增,1历史,此时是新增0
			data['taskType'] =  2//任务类型 0 常规巡视 1 特殊巡视 2 保电值守 3 线路检修
			data['taskId'] =  this.$route.query.taskId
			data['lineTowerId'] =  this.$route.query.lineTowerId
			data['towerTaskId'] =  this.$route.query.towerTaskId
			//上传数据接口
			callData('/accountServer/CmYhLb/appAddYh', 'post', data).then(data => {
				if (data.success) {//上传数据成功之后上传照片
					_this.addPhotos(data, files)
				}
			})
		},
		addPhotos(data, file){//{type1:{file:[]}, type2:{file:[]}}
			const _this = this
			let allConfig = []
			let files = {}
			for (var key in file) {
				const files = file[key];
				allConfig = this.photoConfig(files,allConfig, data);
			}
			if (allConfig.length > 0) {
				all(allConfig,(data)=>{
					//返回上一页
					//_this.back()
					if(data[0].success){
						Toast("上传成功！")
						this.$router.go(-1)
					} else {
						Toast.clear();
						Toast("上传失败！")
					}
				})
			} else {
				// 没有照片直接返回
				//_this.back()
				Toast("上传成功！")
				this.$router.go(-1) 
			}
    	},
    	photoConfig(files, allConfig, data) {
    		for(var i=0;i < files.file.length; i++){
    			if(window.Cordova){
    				let param = {}
    				let filePath = files.file[i]
					param.taskId = this.$route.query.taskId
					param.towerTaskId = this.$route.query.towerTaskId
					param.towerId = this.$route.query.towerId
					param.lineTowerId = this.$route.query.lineTowerId
					param.yhId = data.data.yhId
					param.yhReviewId = data.data.yhReviewId
					param.jd = files.jdwd[i].lon
					param.wd = files.jdwd[i].lat
					param.pictureType = files.pictureType
					allConfig.push(fileData('/fileserver/CmYhPicture/saveCmYhPicture', 'post', param,filePath))
    			}else{
    				let param = new FormData()
    				param.append('file',files.file[i])
					param.append('taskId',this.$route.query.taskId)
					param.append('towerTaskId',this.$route.query.towerTaskId)
					param.append('towerId',this.$route.query.towerId)
					param.append('lineTowerId',this.$route.query.lineTowerId)
					param.append('yhId',data.data.yhId)
					param.append('yhReviewId',data.data.yhReviewId)
					param.append('jd',files.jdwd[i].lon)
					param.append('wd',files.jdwd[i].lon)
					param.append('pictureType',files.pictureType)
					allConfig.push(fileData('/fileserver/CmYhPicture/saveCmYhPicture', 'post', param))
    			}
    		}
    		return allConfig;
    	},
    	back() {
    		//返回页面
			let query = {
				zsTaskId: this.$route.query.zsTaskId,
				towerId:this.$route.query.towerId,
				lineId:this.$route.query.lineId,
				lineName:this.$route.query.lineName,
				towerName:this.$route.query.towerName,
				taskId :this.$route.query.taskId,
				lineTowerId :this.$route.query.lineTowerId,
				towerTaskId:this.$route.query.towerTaskId,
				page: this.$route.query.page,
				size: this.$route.query.size
			}
			this.$router.push({
				name:'bdzs_yinhuanList',
				query: query
			})
    	}
		
	},
	mounted() {
		this.homeShow();
	}
}
</script>
